package com.pesking.core.sqlparser.processor;

import java.util.Arrays;
import java.util.List;

import com.pesking.core.parameter.Parameter;
import com.pesking.core.sqlparser.parameter.SqlParameter;
import com.pesking.core.sqlparser.parameter.SqlParameterProcessor;
import com.pesking.core.sysparam.SystemParameterManage;
import com.pesking.core.util.StringUtils;

public class SystemParameterSqlParameterHandler extends SqlParameterProcessor {

	@Override
	public List<String> getSqlParameterType() {
		return Arrays.asList(new String []{"SP","SYSTEM_PARAMETER"});
	}

	@Override
	public void handle(StringBuilder sbSQL, Parameter parameter, List<SqlParameter> processors,SqlParameter sqlParameter) {
		String key = sqlParameter.getName();
		String defaultValue = sqlParameter.getField();
		if (!StringUtils.isBlank(key)){
			String value = SystemParameterManage.getString(key);
			
			if (value == null && defaultValue != null){
				value = defaultValue;
			}
			
			if (value == null){
				value = "";
			}
			else{
				value = this.formatValue(value);
			}
			sbSQL.append(value);
		}
		else {
			if (defaultValue == null) {
				defaultValue = "";
			}
			sbSQL.append(defaultValue);
		}
	}
}
